package com.cskaoyan.order.service.impl;

import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangminghui
 * @date 2022/8/25
 */
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter orderConverter;


    @Override
    public OrderListResponse orderList(OrderListRequest orderListRequest) {
        PageHelper.startPage(orderListRequest.getPage(), orderListRequest.getSize());
        Example example = new Example(Order.class);
        if (orderListRequest.getSort() != null) {
            example.setOrderByClause(orderListRequest.getSort() + "");
        } else {
            // 如果sort为null，则按订单生成时间降序排列
            //example.setOrderByClause("createTime desc");
            example.setOrderByClause("create_time desc");
        }
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", orderListRequest.getUserId());
        //criteria.andEqualTo("user_id", orderListRequest.getUserId());
        List<Order> orderList = orderMapper.selectByExample(example);
        if (orderList != null) {
            ArrayList<OrderDetailInfo> orderDetailInfoList = new ArrayList<>();
            for (Order order : orderList) {
                OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);
                List<OrderItem> items = orderItemMapper.queryByOrderId(order.getOrderId());
                List<OrderItemDto> dtoList = orderConverter.item2dto(items);
                orderDetailInfo.setOrderItemDto(dtoList);

                OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(order.getOrderId());
                OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
                orderDetailInfo.setOrderShippingDto(orderShippingDto);
                orderDetailInfoList.add(orderDetailInfo);
            }
            // 查询出result中的total
            //Long total = orderMapper.countAll();
            Long total = orderMapper.queryOrderNumByUserId(orderListRequest.getUserId());
            //return orderDetailInfoList;
            OrderListResponse orderListResponse = new OrderListResponse();
            orderListResponse.setData(orderDetailInfoList);
            orderListResponse.setTotal(total);
            return orderListResponse;
        } else {
            return null;
        }
    }

    @Override
    public OrderDetailResponse orderDetail(String orderId) {
        Order order = orderMapper.selectByPrimaryKey(orderId);
        OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
        OrderDetailResponse orderDetailResponse = orderConverter.order2res(order, orderShipping);
        List<OrderItem> items = orderItemMapper.queryByOrderId(orderId);
        List<OrderItemDto> orderItemDtoList = orderConverter.item2dto(items);
        orderDetailResponse.setGoodsList(orderItemDtoList);
        //orderDetailResponse.setUserName(orderShipping.getReceiverName());
        //orderDetailResponse.setTel(orderShipping.getReceiverPhone());
        if (orderDetailResponse != null) {
            return orderDetailResponse;
        } else {
            return null;
        }
    }


}
